package com.douyang.service.impl;

import com.douyang.base.model.UserBaseInfo;
import com.douyang.base.utils.UserUtil;
import com.douyang.model.OrderStatementLog;
import com.douyang.service.OrderStatementLogService;
import com.douyang.service.OrderStatementService;
import com.douyang.service.SaveLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service("createOrderStatement")
public class SaveCreateOrderStatementLogServiceImpl implements SaveLogService {
    @Autowired
    OrderStatementLogService orderStatementLogService;


    @Override
    public Object saveLog(ProceedingJoinPoint joinPoint) throws Throwable {
        UserBaseInfo loginUserBaseInfo = UserUtil.getLoginUserBaseInfo();
        OrderStatementLog orderStatementLog = new OrderStatementLog();
        orderStatementLog.setOps(loginUserBaseInfo.getUserName()+"("+loginUserBaseInfo.getId()+")"+" 尝试为订单号："+joinPoint.getArgs()[0]+" 创建订单流水号");
        orderStatementLog.setCreateTime(LocalDateTime.now());
        Object proceed=null;
        try {
            proceed = joinPoint.proceed();
            orderStatementLog.setSuccess(true);
            orderStatementLog.setResult("创建成功：流水号为"+proceed);
            orderStatementLog.setOrderStatementId((Long) proceed);
        }catch (Throwable e){
            orderStatementLog.setSuccess(true);
            orderStatementLog.setResult("创建失败，"+e.getMessage());
            throw e;
        }finally {
            orderStatementLogService.save(orderStatementLog);
        }
        return proceed;
    }
}
